package org.flexthinker.binding.value.support
{
    import org.flexthinker.binding.conversion.Converter;
    import org.flexthinker.binding.value.ValueModel;

    /**
     *  A value model wrapper that supports converting the wrapped value to and from
     *  another type using the supplied converter.
     *  It can also be used to convert a value of the same type, from a format into another format, when needed.
     *
     *  @see org.flexthinker.binding.conversion.Converter
     *  @see org.flexthinker.binding.conversion.support.PropertyConverter
     */
    public class TypeConverter extends AbstractValueModelProxy
    {
        private var converter:Converter;

        public function TypeConverter(wrappedVM:ValueModel, converter:Converter)
        {
            super(wrappedVM);
            this.converter = converter;
        }


        override public function getValue():Object
        {
            //TBD : error handling for conversion
            return converter ? converter.convertReverse(super.getValue()) : super.getValue();
        }


        override public function setValue(value:Object):void
        {
            // TBD: error handling for conversion
            super.setValue(converter ? converter.convertForward(value) : value );
        }
    }
}
